/**
 *    Copyright 2006-2017 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.generator.plugins.custome;

import org.mybatis.generator.internal.util.StringUtility;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zuoshuai on 2017/1/3.
 */
public class CustomerConstants {
    //*****************service层方法列表***************************************
    public static final String SERVICE_ADD_ONE = "add%s";
    public static final String SERVICE_DELETE_BY_ID = "delete%sByIdLogic";
    public static final String SERVICE_SELECT_BY_ID = "select%sById";
    public static final String SERVICE_SELECT_PAGE = "select%sPage";
    public static final String SERVICE_UPDATE_BY_ID = "update%sById";
    public static final String SERVICE_SELECT_BY_EXAMPLE = "select%sByExample";
    public static final String SERVICE_BUILD_QUERY = "buildQuery";
    //****************controller层 mapping列表*************************************
    public static final String CONTROLLER_SELECT_BY_PAGE_MAPPING = "select_by_page";
    public static final String CONTROLLER_SELECT_BY_Example_MAPPING = "select_by_example";
    public static final String CONTROLLER_SELECT_BY_ID_MAPPING = "select_by_id";
    public static final String CONTROLLER_UPDATE_BY_ID_MAPPING = "update_by_id";
    public static final String CONTROLLER_DELETE_BY_ID_MAPPING = "delete_by_id";
    public static final String CONTROLLER_ADD_MAPPING = "add";
    //*******************controller methods
    public static final String CONTROLLER_SELECT_BY_PAGE = "selectPage";
    public static final String CONTROLLER_SELECT_BY_Example = "selectByExample";
    public static final String CONTROLLER_SELECT_BY_ID = "selectById";
    public static final String CONTROLLER_UPDATE_BY_ID = "updateOnebyId";
    public static final String CONTROLLER_DELETE_BY_ID = "deleteOneById";
    public static final String CONTROLLER_ADD = "addOne";

    /**
     * 生成方法
     *
     * @param constants
     * @param modelName
     * @return
     */
    public static String generateMethod(String constants, String modelName) {
        return String.format(constants, modelName);
    }

    //--------------------方法返回值----------------------------------------------
    public static String generateDTOResult(String modelName) {
        return "Result<" + modelName + "DTO>";
    }

    public static String generateListDTOResult(String modelName) {
        return "Result<List<" + modelName + "DTO>>";
    }

    public static String generatePageDTOResult(String modelName) {
        return "Result<PageQuery<" + modelName + "DTO>>";
    }

    public static String generateBooleanResult() {
        return "Result<Boolean>";
    }

    public static String generateIntResult() {
        return "Result<Integer>";
    }

    /**
     * addUserAccount-->add_user_account
     *
     * @param param
     * @return
     */
    public static String generateRequestMapping(String param) {
        Pattern p = Pattern.compile("[A-Z]");
        if (param == null || param.equals("")) {
            return "";
        }
        StringBuilder builder = new StringBuilder(param);
        Matcher mc = p.matcher(param);
        int i = 0;
        while (mc.find()) {
            builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().toLowerCase());
            i++;
        }
        if ('_' == builder.charAt(0)) {
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }

    public static String firstCharToLowCase(String str) {
        char[] chars = new char[1];
        //String str="ABCDE1234";
        chars[0] = str.charAt(0);
        String temp = new String(chars);
        if (chars[0] >= 'A' && chars[0] <= 'Z') {
            return str.replaceFirst(temp, temp.toLowerCase());
        }
        return str;
    }

    public static String firstCharToUpperCase(String str) {
        char[] chars = new char[1];
        chars[0] = str.charAt(0);
        String temp = new String(chars);
        if (chars[0] >= 'a' && chars[0] <= 'z') {
            return str.replaceFirst(temp, temp.toUpperCase());
        }
        return str;
    }
    //------------------controller 方法返回值-------------------------------------------

    public static void main(String[] args) {
//        System.out.println(transfer2parameter("A"));
        System.out.println(generateRequestMapping("testAddUserName"));
    }
}
